﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Net.Sockets;
using System.Threading;
using System.Net;

using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketBase.Logging;

using SuperSocket.QuickStart.TelnetServer_AppServer;
namespace server
{
    public partial class Form1 : Form
    {
        int gPort;
        String gName;
        String gip;
        Dictionary <string,User> mUsers = new Dictionary<string,User>();
        Dictionary<int, combat> mCombats = new Dictionary<int, combat>();
        Dictionary<string, Maticsoft.Model.Skill> skills = new Dictionary<string, Maticsoft.Model.Skill>();

        Dictionary<string,baseskill> baseskills = new Dictionary<string, baseskill>();
        TelnetServer appServer;

        public Form1()
        {
            InitializeComponent();
            initGame();
        }
        private void initGame()
        {
            String temp = ConfigurationManager.AppSettings["port"];
            gPort = int.Parse(temp);
            gName = ConfigurationManager.AppSettings["name"];
            gip = ConfigurationManager.AppSettings["ip"];
            this.Text = gName + "   " +gip +":" + gPort.ToString();

            appServer = new TelnetServer();

            var serverConfig = new ServerConfig();
            serverConfig.Port = gPort;

            //Setup the appServer
            if (!appServer.Setup(serverConfig))
            {

                return;
            }

            //Try to start the appServer
            if (!appServer.Start())
            {

                return;
            }


            loadSkill();

            loadBaseSkill();

            appServer.Baseskills = baseskills;
            appServer.Skills = skills;

            return;
        }

        void loadBaseSkill()
        {
            baseskill wuzhe = new wuzhetianfu();
            wuzhe.creat();
            baseskills.Add(wuzhe.getName(), wuzhe);

            baseskill fangyu = new fangyuzhetianfu();
            fangyu.creat();
            baseskills.Add(fangyu.getName(), fangyu);
        }

        void loadSkill()
        {
            Maticsoft.DAL.Skill bll = new Maticsoft.DAL.Skill();
            DataSet ds = bll.GetList("");
            foreach (DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Maticsoft.Model.Skill model = bll.DataRowToModel(dr);
                    skills.Add(model.name, model);
                }
            }
        }



        void UpdateUserEventHandler(string name, User aUser,int flag)
        {
            if (flag == 0)
            {
                if (mUsers.ContainsKey(name))
                {
                    mUsers[name].quitUser();
                    mUsers.Remove(name);
                }
                this.mUsers.Add(name,aUser);
            }
            else if(flag == 1)
            {
                if (mUsers.ContainsKey(name))
                {
                    this.mUsers.Remove(name);
                }
            }            
        }

        private void btnSkill_Click(object sender, EventArgs e)
        {
            skill_manage sk_manage = new skill_manage();
            sk_manage.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.appServer.MAllcombat.do_combat();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            money m_money = new money();
            m_money.Show();
        }

    }

}
